<template>
  <div>
    <!--     搜索条件-->
    <SearchForm @search="getTableData" @reset="clearSearch">
      <el-col :span="8" :xs="24" v-if="$permiss[510]">
        <el-form-item :label="$t('所属租户')" prop="adminUserId">
          <el-select v-model="page.adminUserId" @change="tenantChange" style="width: 100%" clearable>
            <el-option v-for="item in tenantList" :key="item.adminUserId" :label="item.adminName"
                       :value="item.adminUserId"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24" v-if="$permiss[509]">
        <el-form-item :label="$t('APP名称')" >
          <el-select style="width: 100%" v-model="page.productId" clearable>
            <el-option v-for="item in productList" :key="item.productId" :label="item.appName"
                       :value="item.productId"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('用户状态')" prop="isNewCustomer">
          <el-select v-model="page.isNewCustomer" style="width: 100%" clearable>
            <el-option v-for="item in userType" :label="item.label" :key="item.value"
                       :value="item.value"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12" :xs="24">
        <el-form-item :label="$t('日期')" prop="timeValue">
          <el-date-picker
              v-model="page.timeValue"
              style="width: 100%"
              type="daterange"
              range-separator="~"
              value-format="yyyy-MM-dd"
              :start-placeholder="$t('开始时间')"
              :end-placeholder="$t('结束时间')">
          </el-date-picker>
        </el-form-item>
      </el-col>
    </SearchForm>

    <el-card shadow="never" style="margin-top: 10px">
      <Table :table-column="tableColumn" :table-data="tableData" :loading="loading" :page="page"
             @handleSizeChange="handleSizeChange"
             @handleCurrentChange="handleCurrentChange"/>
    </el-card>
  </div>
</template>

<script>
import {dateFormatSection, defaultTimeDate} from "@/utils/tools";
import {selectTenant, selectProduct} from "@/api/system";
import {guestGroupList, todayExpireDataExpirePgeList} from '@/api/postLoan'

export default {
  name: 'reminderReport',
  data() {
    return {
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
        timeValue: defaultTimeDate(14)
      },
      productList: [],
      tenantList: [],
      userType: [
        {label: this.$t('新客'), value: 0},
        {label: this.$t('老客'), value: 1},
      ],
      loading: false,
      tableData: [],
      tableColumn: [
        {label: this.$t('日期'), prop: 'dateStr', minWidth: 100, fixed: 'left'},
        {label: this.$t('新增订单数'), prop: 'newOrderNum', minWidth: 150},
        {label: this.$t('新增金额'), prop: 'newAmount'},
        {label: this.$t('新增合同金额'), prop: 'newPassAmount', minWidth: 150},
        {label: this.$t('新增利息'), prop: 'newInterest'},
        {label: this.$t('新增订单结清数'), prop: 'newSettleNum', minWidth: 150},
        {label: this.$t('新增订单结清率'), prop: 'newOrderSettleRate', minWidth: 150},
        {label: this.$t('未结清订单数'), prop: 'notSettleNum', minWidth: 150},
        {label: this.$t('未结清金额'), prop: 'notSettleAmount', minWidth: 150},
        {label: this.$t('未结清合同金额'), prop: 'notSettlePassAmount', minWidth: 150},
        {label: this.$t('未结清利息'), prop: 'notSettleInterest', minWidth: 150},
      ]
    }
  },
  mounted() {
    this.getTableData()
    selectTenant().then(res => {
      this.tenantList = res.data
    })
    selectProduct().then(res => {
      this.productList = res.data
    })
  },
  methods: {
    async getTableData() {
      this.loading = true
      const timeValue = dateFormatSection(this.page.timeValue)
      const params = Object.assign({}, this.page, {
        startTime: timeValue && timeValue.length > 1 ? timeValue[0] : undefined,
        endTime: timeValue && timeValue.length > 1 ? timeValue[1] : undefined
      })
      delete params.timeValue
      const res = await todayExpireDataExpirePgeList(params)
      if (res.code === 1) {
        const {list, total} = res.data
        this.tableData = list.map(item => {
          let {newOrderSettleRate} = item
          newOrderSettleRate = newOrderSettleRate + '%'
          return {...item, newOrderSettleRate}
        })
        this.loading = false
        this.page = Object.assign({}, this.page, {
          total: total
        })
      }
    },
    handleCurrentChange(val) {
      this.page.pageNum = val
      this.getTableData()
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getTableData()
    },
    tenantChange(value) {
      selectProduct({adminUserId: value}).then(res => {
        this.productList = res.data
        this.$set(this.page, 'productId', null)
      })
    },
    clearSearch() {
      this.page = {
        pageNum: 1,
        pageSize: 10,
        total: 0
      }
      this.getTableData()
    }
  }
}
</script>

<style scoped>

</style>
